﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if NETFX_CORE
using Windows.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;
#else
using System.Windows.Media.Imaging;
#endif
using Cofe.Core.Dynamic;
using Cofe.Core.Implements;
using Cofe.Core.Property;
using Cofe.Core.Utils;
using System.Xml.Serialization;

namespace Cofe.Core.EntryType
{
#if UseDataContractToSerialize
    [DataContract(Name="EntryType", Namespace=CofeConstants.Namespace)]
#else
    [XmlRoot(ElementName = "EntryType", Namespace = CofeConstants.Namespace)]
#endif
    public class EntryTypeInfo : CofeSystemInfo, IEntryTypeInfo
    {
        #region Constructor

        public EntryTypeInfo(IPropertyHost properties, IDynamicPropertyInterfaceProvider[] providers)
            : base(properties, providers)
        {
        }

        public EntryTypeInfo(IPropertyHost properties)
            : base(properties)
        {

        }

        protected EntryTypeInfo()
            : base()
        {

        }
        #endregion

        #region Methods

        BitmapContainer getIconContainer(object property)
        {
            byte[] bytes = getIconBytes(property);
            if (bytes.Length == 0)
                return null;
            var sc = StreamContainer.FromByte(bytes);
            return BitmapContainer.FromStream(sc, null, property);
        }

        void setIconContainer(object property, object value)
        {
            if (Properties.State == PropertyHostState.ORMtoCache)
                Properties.Behaviors.SetProperty(property, value);
        }

        byte[] getIconBytes(object property)
        {
            if (Properties.SupportedDefinitions.IsPropertySupported(property))
            {
                //using (var icon = Properties.Behaviors.GetProperty<BitmapContainer>(property))
                var icon = Properties.Behaviors.GetProperty<BitmapContainer>(property);
                using (var stream = icon.GetStream(MediaType.Png))
                {
                    var bytes = stream.GetByte();
                    return bytes;
                }
            }
            else return new byte[] { };
        }

        void setIconBytes(object property, byte[] value)
        {
#if !NETFX_CORE
            if (value != null && value.Length > 0)
            {
                //using (var icon = BitmapContainer.FromBitmapSource(value.ToBitmapSource(), null, property))
                var icon = BitmapContainer.FromBitmapSource(value.ToBitmapSource(), null, property);
                {
                    setIconContainer(property, icon);
                }
            }
#endif
        }

        #endregion

        #region Public Properties

        public string EntryTypeKey
        {
            get { return Properties.Behaviors.GetProperty(CofeEntryTypeProperties.EntryTypeKey).ValueAs<string>(); }
            protected set { Properties.Behaviors.SetProperty(CofeEntryTypeProperties.EntryTypeKey, value); }
        }

        #region IEntryTypeInfo Icon implementations

        BitmapContainer IEntryTypeInfo.SmallIcon
        {
            get { return getIconContainer(CofeIconProperties.SmallIcon); }
            set { setIconContainer(CofeIconProperties.SmallIcon, value); }
        }


        BitmapContainer IEntryTypeInfo.LargeIcon
        {
            get { return getIconContainer(CofeIconProperties.LargeIcon); }
            set { setIconContainer(CofeIconProperties.LargeIcon, value); }
        }

        BitmapContainer IEntryTypeInfo.ExtraLargeIcon
        {
            get { return getIconContainer(CofeIconProperties.ExtraLargeIcon); }
            set { setIconContainer(CofeIconProperties.ExtraLargeIcon, value); }
        }

        BitmapContainer IEntryTypeInfo.JumboIcon
        {
            get { return getIconContainer(CofeIconProperties.JumboIcon); }
            set { setIconContainer(CofeIconProperties.JumboIcon, value); }
        }
        #endregion

        #region ORM Icon implemtations

        public byte[] SmallIcon
        {
            get { return getIconBytes(CofeIconProperties.SmallIcon); }
            set { setIconBytes(CofeIconProperties.SmallIcon, value); }
        }

        public byte[] LargeIcon
        {
            get { return getIconBytes(CofeIconProperties.LargeIcon); }
            set { setIconBytes(CofeIconProperties.LargeIcon, value); }
        }

        public byte[] ExtraLargeIcon
        {
            get { return getIconBytes(CofeIconProperties.ExtraLargeIcon); }
            set { setIconBytes(CofeIconProperties.ExtraLargeIcon, value); }
        }

        public byte[] JumboIcon
        {
            get { return getIconBytes(CofeIconProperties.JumboIcon); }
            set { setIconBytes(CofeIconProperties.JumboIcon, value); }
        }

        #endregion

        #endregion

    }
}
